home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeeds.ans.net!btco!newsadm
- From: Shalom Reich <sqr1874@acf4.nyu.edu>
- Newsgroups: comp.lang.c++,rb.technical
- Subject: Re: Can copy constructor and operator= share code?
- Date: Thu, 29 Feb 1996 09:42:43 -0500
- Organization: Bankers Trust Company
- Message-ID: <3135BB63.4459@acf4.nyu.edu>
- References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM>
- NNTP-Posting-Host: algsvw0058.btco.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (WinNT; I)
-
- Boris Burtin wrote:
- >
- > I have noticed that a copy constructor and operator= perform pretty
- > much the same function. The code I wrote for my class simply copies
- > each member variable from one class to the other.
- > First - if all you are doing in a copy ctor and operator= is copying
- members (i.e. bitwise copy) the compiler will construct these two
- functions for you.
-
- The main purpose of these functions should be to implement the
- *semantics* of copying when all of the data needed by an object
- of the class is not contained in the members. One *usually* needs
- to define these functions when one or more members are *pointers*
- to additional data that is part of the class semantics.
-
- Second - once one or more of the members ar pointers, the operator=
- needs to consider (a) assignment to self (b) cleaning up what was
- originally pointed to by the member of "this".
-
- Third - it is always preferable to initialize members in the any ctor
- (including the copy ctor) with the initialization list rather than with
- code in the body of the ctor.
-
- A good book that covers these issues (in a very readable style) is
- "Effective C++" by Scott Meyer. Addison-Wesley.
-
- Shalom Reich
-